package com.kaili.basic.config;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
* mybatis-plus 自定义填充公共字段
* @author:zhengs
* @Time: 19-1-25 下午12:07
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private static final String CREATE_BY = "createBy";
    private static final String MODIFY_BY = "modifyBy";

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        Date now = new Date();
        if(metaObject.hasSetter(CREATE_BY) && null == metaObject.getValue(CREATE_BY)){
            Integer userId =  LoginDetailUtils.getCurrentEmployeeId();
            this.setFieldValByName("createBy",userId,metaObject);
            this.setFieldValByName("createTime",now,metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        if (metaObject.hasSetter(MODIFY_BY)) {
            Integer userId =  LoginDetailUtils.getCurrentEmployeeId();
            this.setFieldValByName("modifyBy", userId,metaObject);
            this.setFieldValByName("modifyTime", new Date(),metaObject);
        }
    }
}